package com.sec.android.app.myfiles.feature.Hover;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.ImageView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.ExifUtils;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ImageHoverView extends AbsHoverView {

    public ImageHoverView(Context context, FileRecord record, AbsMyFilesFragment fragment) {
        super(context, record, fragment);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.hover_image_view;
    }

    @Override
    protected void initView() {
        ImageView previewThumbnail = (ImageView) mHoverView.findViewById(R.id.airview_preview_image);
        //TODO: Move it AbsHoverView when modifying VideoHoverView to inherit AbsHoverView
        mAirViewMaxWidth = mContext.getResources().getDimension(R.dimen.airview_preview_max_width);
        mAirViewMaxHeight = mContext.getResources().getDimension(R.dimen.airview_preview_max_height);
        Bitmap thumbnailBm = getBmpFromFile(mRecord, mAirViewMaxWidth, mAirViewMaxHeight);

        if (thumbnailBm != null) {
            float ratio = getRatio((float) thumbnailBm.getWidth(), (float) thumbnailBm.getHeight());
            if (ratio != 0 && ratio != 1.0f) {
                Bitmap oriBm = thumbnailBm;
                thumbnailBm = Bitmap.createScaledBitmap(thumbnailBm, (int) (thumbnailBm.getWidth() * ratio),
                        (int) (thumbnailBm.getHeight() * ratio), true);
                if (!thumbnailBm.equals(oriBm)) {
                    oriBm.recycle();
                }

                int rotation = ExifUtils.getRotationAngleByExif(ExifUtils.getExif(mRecord.getFullPath()));
                if (rotation != 0) {
                    thumbnailBm = ExifUtils.getRotateBitmap(thumbnailBm, rotation);
                }
            }
        } else {
            Bitmap tempThumbnail = ExifUtils.createThumbnailFromEXIF(mRecord, (int) (mAirViewMaxWidth), (int) (mAirViewMaxWidth * mAirViewMaxHeight));
            if (tempThumbnail != null) {
                float ratio = getRatio((float) tempThumbnail.getWidth(), (float) tempThumbnail.getHeight());
                thumbnailBm = Bitmap.createScaledBitmap(tempThumbnail, (int) (tempThumbnail.getWidth() * ratio),
                        (int) (tempThumbnail.getHeight() * ratio), true);
                if (!thumbnailBm.equals(tempThumbnail)) {
                    tempThumbnail.recycle();
                }
            }
        }

        if (mRecord.getFileType() == FileType.PNG || mRecord.getFileType() == FileType.GIF) {
            previewThumbnail.setBackgroundColor(Color.BLACK);
        }

        if (thumbnailBm == null) {
            previewThumbnail.setImageResource(MediaFile.getIcon(mContext, mRecord));
        } else {
            previewThumbnail.setImageBitmap(thumbnailBm);
        }
    }

    private Bitmap getBmpFromFile(FileRecord record, float thumbnailWidth, float thumbnailHeight) {
        Bitmap bitmap = null;

        try (FileInputStream stream = new FileInputStream(record.getFullPath())) {
            FileDescriptor fd = stream.getFD();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 1;
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(fd, null, options);

            if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
                return null;
            }
            // Determine how much to scale down the image
            options.inSampleSize = (int) Math.min(options.outWidth / thumbnailWidth, options.outHeight / thumbnailHeight);
            options.inJustDecodeBounds = false;
            options.inDither = false;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
        } catch (FileNotFoundException e) {
            Log.e(this, "FileNotFoundException:" + e.toString());
        } catch (IOException e) {
            Log.e(this, "IOException:" + e.toString());
        } catch (OutOfMemoryError e) {
            Log.e(this, "OutOfMemoryError:" + e.toString());
        }

        return bitmap;
    }

    @Override
    protected void onClose() {
        //Do nothing
    }

    private float getRatio(float width, float height) {
        //TODO: Move it AbsHoverView when modifying VideoHoverView to inherit AbsHoverView
        float ratio;

        if ((mAirViewMaxWidth / width) > (mAirViewMaxHeight / height)) {
            ratio = mAirViewMaxHeight / height;
        } else {
            ratio = mAirViewMaxWidth / width;
        }

        return ratio;
    }
}
